package com.github.livebase.util.base;

import java.util.Iterator;

public class CharIterator implements Iterator<Character> {
        private char[] cs;
        private int idx = 0;
        public CharIterator(String str) {
            this(str.toCharArray());
        }
        private CharIterator(char[] cs) {
            this.cs = cs;
        }

        @Override
        public boolean hasNext() {
            return idx < cs.length;
        }

        @Override
        public Character next() {
            return cs[idx++];
        }

        public boolean hasPrevious() {
            return idx > 0;
        }

        public Character previous() {
            return cs[--idx];
        }
        public static String readUntil(CharIterator iterator, char... endTag) {
            StringBuilder builder = new StringBuilder();
            while (iterator.hasNext()) {
                Character next = iterator.next();
                if (match(next, endTag)) {
                    break;
                }
                builder.append(next);
            }
            return builder.toString();
        }

        private static boolean match(char next, char[] endTag) {
            for (char c : endTag) {
                if (c == next) {
                    return true;
                }
            }
            return false;
        }
    }